package com.itheima.controller.ums;


import com.itheima.core.PageInfo;
import com.itheima.core.R;
import com.itheima.pojo.ums.UmsAdmin;
import com.itheima.pojo.ums.UmsRole;
import com.itheima.pojo.ums.vo.LoginParamVo;
import com.itheima.pojo.ums.vo.UmsAdminVo;
import com.itheima.pojo.ums.vo.UpdatePasswordVo;
import com.itheima.service.ums.UmsAdminService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Slf4j
public class UmsAdminController {

    @DubboReference
    private UmsAdminService umsAdminService;


    /**
     * 登录以后返回token
     *
     * @param loginParamVo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginParamVo loginParamVo) {
        log.info(loginParamVo.toString());

        Map<String, String> map = new HashMap<>();
        String jwt = umsAdminService.login(loginParamVo);
        map.put("token", jwt);

        return R.success("登录成功", map);
    }

    @GetMapping("/info")
    private R info() {
        UmsAdminVo admin = umsAdminService.info("admin");
        return R.success(admin);
    }

    @PostMapping("/logout")
    public R logout() {
        //umsAdminService.logout();

        return R.success();
    }

    @GetMapping ("/list")
    public R list( String keyword,int pageNum, int pageSize) {

        log.info("条件搜索的参数"+keyword);
        PageInfo umsAdminPage = umsAdminService.pageList(keyword, pageNum, pageSize);
        return R.success(umsAdminPage);
    }

    @GetMapping("/role/{adminId}")
    public R getRoles(@PathVariable Long adminId) {
        List<UmsRole> list = umsAdminService.getRoles(adminId);

        return R.success("操作成功",list);
    }

    @PostMapping("/role/update")
    public R roleUpdate(@RequestParam Long adminId,@RequestParam List<Long> roleIds) {
        log.info(adminId+".."+roleIds.toString());
        umsAdminService.roleUpdate(adminId, roleIds);
        return R.success("操作成功");
    }


    @PostMapping("/update/{id}")
    public R updateAdmin(@PathVariable Long id,@RequestBody UmsAdmin umsAdmin){
        umsAdminService.updateAdmin(id,umsAdmin);
        return R.success("操作成功");
    }

    @PostMapping("/updateStatus/{id}")
    public R updateStatus(@PathVariable Long id,@RequestParam Integer status){
        umsAdminService.updateStatus(id,status);
        return R.success("操作成功");
    }

    @GetMapping("/{id}")
    public R getUmsAdminInfo(@PathVariable Long id){
        UmsAdmin umsAdmin=umsAdminService.getUmsAdminInfo(id);
        return R.success("操作成功",umsAdmin);
    }


    @PostMapping("/updatePassword")
    public R updatePassword(@RequestBody UpdatePasswordVo updatePasswordVo){
        umsAdminService.updatePassword(updatePasswordVo);
        return R.success("操作成功");
    }


    @PostMapping("/delete/{id}")
    public R delete(@PathVariable Long id) {
        umsAdminService.delete(id);
        return R.success("操作成功");
    }
}
